package com.smcaiot.jfttask.domain;

import com.smcaiot.core.exception.DomainException;

/**
 * @Author: cz
 * @Date: 2023/10/8
 * @Description:
 */
public enum CycleLength {
    TASK_CYCLE_LENGTH_DAY(1, "日循环任务"),
    TASK_CYCLE_LENGTH_WEEK(7, "周循环任务"),
    TASK_CYCLE_LENGTH_MONTH(30, "月循环任务"),
    TASK_CYCLE_LENGTH_QUARTER(90, "季循环任务"),
    TASK_CYCLE_LENGTH_HALFYEAR(180, "半年循环任务"),
    TASK_CYCLE_LENGTH_YEAR(365, "年循环任务");

    private int code;
    private String name;

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    CycleLength(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public static CycleLength getByCode(int code) {
        for (CycleLength length : CycleLength.values()) {
            if (length.getCode() == code) {
                return length;
            }
        }
        throw new DomainException(String.format("未找到长度为 %d 的周期", code));
    }
}
